{{/*
  style/GetMainCSS
  Process the main css stylesheet and return as resource

  @author @regisphilibert

  @context Any (.)

  @returns Resource

  @uses
     - func/style/GetResource
*/}}
{{ $main_style := dict }}

{{/* We prepare a slice of resources to be concatenated as one */}}
{{ $assets_to_concat := collections.Slice }}
{{/* We add locale css files to the slice in the proper order */}}
{{ range collections.Slice "_tachyons.css" "_code.css" "_hugo-internal-templates.css" "_social-icons.css" "_styles.css" }}
  {{ with partials.IncludeCached "func/style/GetResource.html" . . }}
    {{ $assets_to_concat = $assets_to_concat | collections.Append . }}
  {{ end }}
{{ end }}

{{- $config := site.Params.ananke.social -}}
{{- $networks := $config.follow.networks -}}
{{- $setups := (collections.Where $config.networks "slug" "in" $networks) }}
{{ with $setups }}
  {{ $socials_rules := collections.Slice }}
  {{ range $service := . }}
    {{ with .color }}
      {{ $rule := fmt.Printf `
        .ananke-socials a.%s:hover {
          color: %s !important;
        }` $service.slug . }}
      {{ $socials_rules = $socials_rules | collections.Append $rule }}
    {{ end }}
  {{ end }}
  {{ with $socials_rules }}
    {{ $socials_rules = collections.Delimit . " " }}
    {{ $socials_css := $socials_rules | resources.FromString "ananke/css/generated_socials.css" }}
    {{ $assets_to_concat = $assets_to_concat | collections.Append $socials_css }}
  {{ end }}
{{ end }}

{{/* We look for any custom css files registered by the user under `site.params.custom_css and if found in the theme's
css asset directory we (unless condition below) add to aforementioned slice */}}
{{ with site.Params.custom_css }}
  {{ range . }}
    {{ with partials.IncludeCached "func/style/GetResource.html" . . }}
      {{ if compare.Eq .MediaType.SubType "x-scss" "x-sass" "scss" "sass" }}
        {{ if hugo.IsExtended }}
          {{/* as we cannot concatenate styles of different types, we sass/scss to be transformed to css beforehand */}}
          {{ $assets_to_concat = $assets_to_concat | collections.Append (. | css.Sass) }}
        {{ else }}
          {{ partials.Include "func/warn.html" (fmt.Printf "Processing of stylesheet %s of type %s has been skipped. You need Hugo Extended to process such files." .Name .MediaType.SubType) }}
        {{ end }}
      {{ else }}
        {{ $assets_to_concat = $assets_to_concat | collections.Append . }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

{{ with $assets_to_concat }}
  {{/* We proceed to concatenate the $assets_to_concat */}}
  {{ $style := . | resources.Concat "ananke/css/main.css" }}

  {{/* We then use toCSS to add sourceMap and minify */}}
  {{ $options := dict "enableSourceMap" true "precision" 6 }}
  {{ $style = $style | css.Sass $options | resources.Minify }}
  {{/* We fingerprint in production for cache busting purposes */}}
  {{ if hugo.IsProduction }}
    {{ $style = $style | resources.Fingerprint }}
  {{ end }}
  {{/* We're ready to set returning variable with resulting resource */}}
  {{ $main_style = $style }}
{{ end }}

{{ return $main_style }}
